﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Common.Library;
using OnlineVideos.Data;
using System.Text.RegularExpressions;
using System.IO.IsolatedStorage;
using OnlineVideos.Entities;
using System.Threading.Tasks;

namespace OnlineVideos.Views
{
    public partial class RemindersAlarms : PhoneApplicationPage
    {
        public RemindersAlarms()
        {
            InitializeComponent();
            Loaded += RemindersAlarms_Loaded;
        }

        void RemindersAlarms_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            if (settings.Contains("UserEmail"))
            {
                if (!string.IsNullOrEmpty(settings["UserEmail"].ToString()))
                    TbEmail.Text = settings["UserEmail"].ToString();
            }
            }
            catch (Exception ex)
            {
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(TbEmail.Text))
                {
                    Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                    Match match = regex.Match(TbEmail.Text);
                    if (match.Success)
                    {
                        tblEmailval.Visibility = Visibility.Collapsed;
                        AppSettings.UserEmail = TbEmail.Text;
                    }
                    else
                    {
                        tblEmailval.Visibility = Visibility.Visible;
                        return;
                    }
                    
                }

                if ((reminderCheckBox.IsChecked == true || alarmCheckBox.IsChecked == true) && (GreatCheckBox.IsChecked == true || LessCheckBox.IsChecked == true))
                {
                    int showid = AppSettings.ShowUniqueID;
                    ReminderTable Rtable = Task.Run(async () => await Constants.connection.Table<ReminderTable>().Where(i => i.ShowID == showid).FirstOrDefaultAsync()).Result;
                    if (Rtable == null)
                    {
                        ReminderTable Rt = new ReminderTable();
                        Rt.ShowID = showid;
                        if (!string.IsNullOrEmpty(TbLess.Text))
                            Rt.LessThan = Convert.ToInt32(TbLess.Text);
                        else
                            Rt.LessThan = 0;
                        if (!string.IsNullOrEmpty(TbGreat.Text))
                        Rt.GreaterThan = Convert.ToInt32(TbGreat.Text);
                        else
                            Rt.GreaterThan = 0;
                        if (reminderCheckBox.IsChecked == true && alarmCheckBox.IsChecked == true)
                        {
                            Rt.Type = "alarm-Reminder";
                        }
                        else
                        {
                            if (alarmCheckBox.IsChecked == true)
                            Rt.Type = "alarm";
                        else
                            Rt.Type = "Reminder";
                        }
                        Constants.connection.InsertAsync(Rt);
                       
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(TbLess.Text))
                            Rtable.LessThan = Convert.ToInt32(TbLess.Text);
                        else
                            Rtable.LessThan = 0;
                        if (!string.IsNullOrEmpty(TbGreat.Text))
                        Rtable.GreaterThan = Convert.ToInt32(TbGreat.Text);
                        else
                            Rtable.GreaterThan = 0;
                        if (reminderCheckBox.IsChecked == true && alarmCheckBox.IsChecked == true)
                        {
                            Rtable.Type = "alarm-Reminder";
                        }
                        else
                        {
                            if (reminderCheckBox.IsChecked == true)
                                Rtable.Type = "alarm";
                            else
                                Rtable.Type = "Reminder";
                        }
                        Constants.connection.UpdateAsync(Rtable);
                    }
                    NavigationService.GoBack();
                }
                
            }
            catch (Exception ex)
            {
            }
        }

    }
}